<template>
	<view class="content">
		<view v-if="pageType=='auth'">
			<view class="solids-bottom padding-xs flex align-center">
				 
				<view class="flex-sub text-center">
					<view class="solid-bottom text-sl padding">
						<text class="text-blue">百应到家</text>
					</view>
					<view class="padding">百应到家为社区家庭提供优质、保障的家政服务；精选优质到家服务，提升生活品质。授权登陆后可以正常使用平台提供的一键呼叫到家服务功能</view>
				</view>
			</view>
			  <view class="box margin-top-20" style=" padding: 20upx;">
			 
				<button @click="auth" class="cu-btn block bg-blue margin-tb-sm lg">
				<text :class="loadding2==1?'cuIcon-loading2 cuIconfont-spin':'cuIconfont-spin'"></text> 确认授权</button>
			 
			</view>
			 
		</view>
		 <form v-else>
			 <view class="cu-form-group">
				<view class="title">手机号码</view>
				<input placeholder="输入手机号码" type="number"  @input="setPhoneNum" name="input"></input>
				<view class="cu-capsule radius">
					<view class='cu-tag bg-blue '>
						+86
					</view>
					<view class="cu-tag line-blue">
						中国大陆
					</view>
				</view>
			</view>
			 <view class="cu-form-group">
				<view class="title">验证码</view>
				<input @input="setPhoneCode" type="number"  placeholder="输入验证码" name="input"></input>
				<button @click="getCode"  class='cu-btn bg-green shadow'>{{getCodeText}}</button>
			</view>
		
		  <view class="box margin-top-20" style=" padding: 20upx;">
			 
				<button @click="dobindphone" class="cu-btn block bg-blue margin-tb-sm lg">
				<text :class="loadding==1?'cuIcon-loading2 cuIconfont-spin':'cuIconfont-spin'"></text> 保存手机号</button>
			 
			</view>
			
		 </form>
	</view>
</template>

<script>
	import beeApp from '../../beeApp.js'
	export default {
		data() {
			return {
				title: 'Hello',
				getCodeText:'获取验证码',
				sendingcode:0,
				loadding:0,
				pageType:"bindphone",
				loadding2:0
			}
		},
		onLoad(option) {
			if(option.page){
				this.pageType = option.page
			}
			if(this.pageType=="auth"){
				uni.setNavigationBarTitle({
					title:'授权登陆'
				})
				 
			}
          // beeApp.alert("yiqwyei");
		},
		methods: {
		 getCode:function(){
  
			  var i =60;
			  var that = this;
			  // var that = $("#codeBnt");
			  if(this.sendingcode==1){
				 return ;
			  }
			  this.sendingcode = 1;
			 //验证码倒计时
			  
			  that.getCodeText = "重新获取("+i+"s)";
			  var t = setInterval(function(){
				 i--;
				 
				  that.getCodeText = "重新获取("+i+"s)";
				 if(i===0){
					clearInterval(t);
					that.getCodeText = "获取验证码";
					that.sendingcode =0;
				 }
				},1000);
			  
			   
			  //加密
			  beeApp.api("apiv2/user/getPhoneCode",{phone:that.phoneNum},function(res){
				 if(res.code!=200){
					beeApp.alert(res.msg);
					that.sendingcode = 0;
					clearInterval(t);
					that.getCodeText = "获取验证码";
				   return ;
				 }
			  });
         },
		  
		 setPhoneNum:function(e){
			 // console.log(e);
			 this.phoneNum = e.detail.value;
		 },
		 auth:function(){
			    var that = this;
				if(that.loadding2==1){
					return;
				}
				that.loadding2=1;
				my.getAuthCode({
					scopes: 'auth_user',
					success: (res) => {
						// that.authCode = res.authCode;
						// that.loadding2=0;
						my.getAuthUserInfo({
							success: (userInfo) => {
								userInfo.authCode = res.authCode;
								beeApp.loginByUserInfo(userInfo);
								uni.switchTab({
									url:'/pages/index/index'
								})
								that.loadding2=0;
							},
						});
						
					},
					fail: function(res) {
						console.log("getAuthCode:", res);
						that.loadding2=0;
					}
				});
				
		 },
		 setPhoneCode:function(e){
			 // console.log(e);
			 this.phoneCode = e.detail.value;
		 },
        dobindphone:function(){
		  var that = this;
		  that.loadding =1;
		  var user_info =  uni.getStorageSync("user_info")
          beeApp.api("apiv2/user/bindPhone",{
			  phone:this.phoneNum,
			  code:this.phoneCode,
			  openid:user_info.openid,
			  method:"POST",
		  },function(res){
			  that.loadding =0;
             if(res.code!=200){
                beeApp.alert(res.msg);
                return ;
             }
			res.data.openid =  user_info.openid;
			uni.removeStorageSync("user_info")
			uni.setStorageSync("user_info", res.data);
			beeApp.setPageData("pages/user/index",{reload:1});
			beeApp.setPageData("pages/index/index",{reload:1});
			uni.navigateBack();
          });
        },
	}
		
}
</script>
